//
//  MyView.m
//  MyFirstTouch
//
//  Created by acco on 08/03/08.
//  Copyright __MyCompanyName__ 2008. All rights reserved.
//

#include <sys/times.h>

#import "MyView.h"
#import "MyMelody.h"

@implementation MyView


const int soundNum = 5;
const int photoCount = 4;

NSString *photos[] = { 
	@"DSC_0253.JPG", 
	@"DSC_0150.JPG", 
	@"DSC_0296.JPG", 
	@"DSC_0245.JPG"
};


// create random score info
-(void) initScore:(int)cardNum numOfScore:(int)scoreNum
{
	int i = 0;
	scoreArray = [[NSMutableArray alloc]init];
	cardArray = [[NSMutableArray alloc]init];	
	srandom(time(NULL));	
	for( i = 0 ; i < cardNum/2; i++ ) { 
		NSMutableArray *score = [[[NSMutableArray alloc] init] autorelease];
		int j = 0;
		for(j = 0;j < scoreNum; j++ ){
			NSNumber *scorenum = [[[NSNumber alloc] initWithInt:(int)(rand() % soundNum)] autorelease];
//			NSNumber *scorenum = [[NSNumber alloc] initWithInt:(int)((rand() / ((double)RAND_MAX+1.0f)) * soundNum)];
			NSDictionary* scoreDict = [[[NSDictionary alloc] initWithObjectsAndKeys:	
										scorenum,			@"score",
										scorenum,			@"start_point",
										scorenum,			@"end_point",
										nil] autorelease];
			if ( scoreDict ) {
				[score addObject:scoreDict];
			}
		}
		[scoreArray addObject:score];
	}
	for (i = 0; i < cardNum; i++) {
		int idx = i;
		if( idx >= cardNum/2 )  { idx -= cardNum/2; }
		[cardArray addObject:[scoreArray objectAtIndex:idx]];
	}
}


-(void) drawRect:(CGRect)rect 
{
	
	if(photoIndex < 0) {
//		photoIndex = (int)rand() % photoCount; // set random photo
		photoIndex = 0;
	}
	UIImage *bgImage2 = [UIImage imageNamed:photos[photoIndex]];
	if(bgImage2) {
		[bgImage2 drawInRect:rect];
	}
	
	if( startP.x > 0.0f && startP.y > 0.0f ) {
#if 0
		CGRect r =[self bounds];
		float tileX = r.size.width/3;
		float tileY = r.size.height/4;
		float panelOrgX = (int)(startP.x / tileX) * tileX;
		float panelOrgY = (int)(startP.y / tileY) * tileY;
		CGPoint atPoint = { panelOrgX, panelOrgY };
		[bgImage2 drawAtPoint:atPoint blendMode:kCGBlendModeNormal alpha:0.5];
#endif		
		[UIView beginAnimations:@"alpha" context:nil]; 
		[UIView setAnimationDelegate: self];
		[UIView setAnimationDuration:0.1];
		[self setAlpha:0.5f];		
		[UIView commitAnimations];
		[self setNeedsDisplay];
	}
	
	if(endP.x > 0.0f && endP.y > 0.0f) {
		[UIView beginAnimations:@"alpha" context:nil]; 
		[UIView setAnimationDelegate: self];
		[UIView setAnimationDuration:0.4];
		[self setAlpha:1.0f];		
		[UIView commitAnimations];
		[self setNeedsDisplay];
		photoIndex = -1;
	}
	
}

- (void) playSound:(int)idx
{
	NSLog(@"%d", idx);
	[NSThread detachNewThreadSelector:@selector(PlayMelody:) toTarget:[MyMelody class] withObject:(NSArray *)[cardArray objectAtIndex:idx]];
	
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

	startP = [[(NSSet *)touches anyObject] locationInView];
	endP.x = -1.0f;
	endP.y = -1.0f;
	CGRect r =[self bounds];
	float x1 = r.origin.x;
	float x2 = r.size.width/3;
	float x3 = r.size.width/3*2;		
	float y1 = r.origin.y;
	float y2 = r.size.height/4;
	float y3 = r.size.height/4*2;		
	float y4 = r.size.height/4*3;		
	int cardidx = 0;
	int yidx = 0;
	
	if( startP.y >= y1 && startP.y < y2) {
		yidx = 1;
	} else if( startP.y >= y2 && startP.y < y3) {
		yidx = 2;		
	} else if( startP.y >= y3 && startP.y < y4) {
		yidx = 3;		
	} else {
		yidx = 4;		
	}
	
	if( startP.x >= x1 && startP.x < x2) {
		switch (yidx) {
			case 1:
				cardidx = 0;
				break;
			case 2:
				cardidx = 1;
				break;
			case 3:
				cardidx = 2;
				break;
			case 4:
				cardidx = 3;
				break;
			default:
				cardidx = 0;
		}
	} else if( startP.x >= x2 && startP.x < x3) {
		switch (yidx) {
			case 1:
				cardidx = 4;
				break;
			case 2:
				cardidx = 5;
				break;
			case 3:
				cardidx = 6;
				break;
			case 4:
				cardidx = 7;
				break;
			default:
				cardidx = 0;
		}
	} else {
		switch (yidx) {
			case 1:
				cardidx = 8;
				break;
			case 2:
				cardidx = 9;
				break;
			case 3:
				cardidx = 10;
				break;
			case 4:
				cardidx = 11;
				break;
			default:
				cardidx = 0;
		}
	}
	[self playSound:cardidx];

	[self setNeedsDisplay];
	
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
	startP.x = -1.0f;
	startP.y = -1.0f;
	endP = [[(NSSet *)touches anyObject] locationInView];

	[self setNeedsDisplay];

}

-(void) setBackGroundImage:(UIImage *)image
{
	bgImage = image;
}

@end
